/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-service-testdriving
*
* @File name : TestDrivingController.java
*
* @Author : Administrator
*
* @Date : 2017年11月8日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年11月8日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.service.testdriving.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.dto.testdrivingAm.TestDrivingAmDto;
import org.marketing.common.dto.testdrivingAm.TestdrivingAmFrecordDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.marketing.common.Response;
import com.yonyou.gmmc.service.testdriving.constant.TestDrivingConstant;
import com.yonyou.gmmc.service.testdriving.service.TestDrivingService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *
 * @author Administrator 试驾预约
 * @date 2017年11月8日
 */

@RestController
@RequestMapping(value = TestDrivingConstant.BASE_PATH + "/" + TestDrivingConstant.VERSION)
@Api("试驾预约相关API")
public class TestDrivingController {

	private Logger logger = Logger.getLogger(TestDrivingController.class);

	@Autowired
	private TestDrivingService testDrivingService;

	/**
	 *
	 * @author LiuJun 试驾预约保存
	 * @date 2016年11月7日
	 * @param testDrivingAmDto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.POST, consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "试驾预约保存", notes = "试驾预约保存")
	public @ResponseBody Response<Map<String, Object>> saveTestDrivingAppointment(
			@RequestBody @Valid TestDrivingAmDto testDrivingAmDto, HttpServletRequest request) {

		String openId = request.getHeader(CommonConstant.HEADER_OPENID);
		try {
			testDrivingAmDto = testDrivingService.saveTestDrivingAppointment(testDrivingAmDto, openId);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			if (testDrivingAmDto != null) {
				resultMap.put("result", "success");
				resultMap.put("appointmentId", testDrivingAmDto.getTestdriveAppointmentId());
			}
			return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
		} catch (Exception e) {
			logger.error("保存状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}

	/**
	 * 
	 * 取得服务顾问试驾预约的信息
	 * 
	 * @author WangHuicheng
	 * @date 2016年11月21日
	 * @param dealerCode
	 *            经销商代码
	 * @param salesConsultantId
	 *            服务顾问ID
	 * @param status
	 *            状态
	 * @param startDate
	 *            预约到店时间区间（FROM/END）
	 * @return
	 */
	@RequestMapping(value = "/getTestDriverInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation(value = "取得服务顾问试驾预约的信息", notes = "取得服务顾问试驾预约的信息")
	public Response<List<TestDrivingAmDto>> getTestDriverInfo(@RequestParam("dealerCode") String dealerCode,
			@RequestParam(required = false, defaultValue = "") Integer salesConsultantId,
			@RequestParam(required = false, defaultValue = "") Integer status,
			@RequestParam(required = false, defaultValue = "") String startDateFrom,
			@RequestParam(required = false, defaultValue = "") String startDateEnd) {
		try {
			List<TestDrivingAmDto> list = testDrivingService.getTestDriverInfo(dealerCode, salesConsultantId, status,
					startDateFrom, startDateEnd);
			return new Response<List<TestDrivingAmDto>>(ResultMsgConstants.RESULT_CODE_SUCCESS, list, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}

	/**
	 *
	 * @author LiuJun 更新试驾预约状态
	 * @date 2016年11月21日
	 * @param testdrivingAmFrecordDto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/status", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "更新试驾预约状态", notes = "更新试驾预约状态")
	public @ResponseBody Response<Map<String, Object>> updateTestDrivingAppointStatus(
			@RequestBody @Valid TestdrivingAmFrecordDto testdrivingAmFrecordDto) throws Exception {
		try {
			testDrivingService.updateTestDrivingAppointStatus(testdrivingAmFrecordDto);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
			resultMap.put(ResultMsgConstants.ERR_MSG, "");
			return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
		} catch (Exception e) {
			logger.error("更新状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}

	/**
	 *
	 * @author LiuJun 保存试驾预约跟进记录
	 * @date 2016年11月21日
	 * @param testDrivingAmDto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/followRecords", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "保存试驾预约跟进记录", notes = "保存试驾预约跟进记录")
	public @ResponseBody Response<Map<String, Object>> saveTestDrivingAppointFrecord(
			@RequestBody @Valid TestdrivingAmFrecordDto testdrivingAmFrecordDto) throws Exception {

		try {
			testdrivingAmFrecordDto = testDrivingService.saveTestDrivingAppointFrecord(testdrivingAmFrecordDto);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
			resultMap.put("tlTestdriverRecodeId", testdrivingAmFrecordDto.getTlTestdriverRecodeId());
			resultMap.put(ResultMsgConstants.ERR_MSG, "");
			return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
		} catch (Exception e) {
			logger.error("保存状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}

	/**
	 * @author QianKun 试驾查询详情
	 * @date 2016年11月15日
	 * @param testdriveAppointmentId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/testDrivingDataById", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "试驾查询详情", notes = "试驾查询详情")
	public Response<List<TestDrivingAmDto>> getTestDrivingDataById(
			@RequestParam("testdriveAppointmentId") String testdriveAppointmentId) throws Exception {
		try {
			List<TestDrivingAmDto> dtoList = testDrivingService.getTestDrivingDataById(testdriveAppointmentId);
			return new Response<List<TestDrivingAmDto>>(ResultMsgConstants.RESULT_CODE_SUCCESS, dtoList, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}

	/**
	 * @author QianKun 试驾查询列表
	 * @date 2016年11月16日
	 * @param potentialUserId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/testDrivingData", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "试驾查询列表", notes = "试驾查询列表")
	public Response<List<TestDrivingAmDto>> getTestDrivingData(
			@RequestParam(required = false, defaultValue = "") String phone,
			@RequestParam(required = false, defaultValue = "") String submitDateFrom,
			@RequestParam(required = false, defaultValue = "") String submitDateEnd,
			@RequestParam(required = false, defaultValue = "") String startDateFrom,
			@RequestParam(required = false, defaultValue = "") String startDateEnd,
			@RequestParam(required = false, defaultValue = "") String appointmentStatus, HttpServletRequest request)
			throws Exception {
		String openId = request.getHeader(CommonConstant.HEADER_OPENID);
		try {
			List<TestDrivingAmDto> dtoList = testDrivingService.getTestDrivingData(openId, phone, submitDateFrom,
					submitDateEnd, startDateFrom, startDateEnd, appointmentStatus);
			return new Response<List<TestDrivingAmDto>>(ResultMsgConstants.RESULT_CODE_SUCCESS, dtoList, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		}
	}

}
